﻿log("Initializing Page Messenger ...")
msg = {}
tasks = {};

self.port.on('msg', onMessage);
self.port.on("loaded", onLoadedMessage);

msg.send = sendMessageToServer;
msg.sendToAddon = sendMessageToAddon;

// one pre-installed tasks : close
tasks.close = function()
{
    msg.sendToAddon("closeMe");
}

tasks.activate = function()
{
    msg.sendToAddon("activateMe");
}

function onMessage(type, arg)
{
    // All message just handle by us
    if (!tasks[type])
        log("<Error> : Task not exist :" + type);
    else
        tasks[type](arg);
}

function sendMessageToServer(type, arg)
{
    self.port.emit('msg', type, arg);
}

function sendMessageToAddon(type, arg)
{
    self.port.emit('addon', type, arg);
}

function onLoadedMessage()
{
    if (tasks["onLoaded"])
        tasks["onLoaded"]();
}